from math import exp,log
import numpy as np



def single_bound(med_X, med_to_end,alph):
    v = -1
    for i in range(len(med_to_end)):
        if i != len(med_to_end)-1 and med_to_end[i] == med_to_end[i+1]:
            continue
        f1 = (abs(med_to_end[:i+1] - med_to_end[i]) / abs( med_to_end[i] - med_X )).sum()  # 隶属度
        v_i = f1 * exp( -1*alph *abs(med_to_end[i] - med_X) )
        if v_i > v:
            v,bound = v_i,med_to_end[i]
    return bound

def solve_bound(X_j,alph):
    X_j = np.sort(X_j)
    print(X_j)
    # 求中值,以med为界，划分区间
    l_Xj = len(X_j)
    if l_Xj % 2 == 0:
        m, n = X_j[ int(l_Xj/2-1) ], X_j[ int(l_Xj/2) ]
        med_to_b = X_j[int(l_Xj/2)+1:]
        a_to_med = X_j[:int(l_Xj/2-1)]
    else:
        m = n = X_j[ int((l_Xj-1)/2) ]
        med_to_b = X_j[int((l_Xj-1)/2)+1:]
        a_to_med = X_j[:int((l_Xj-1)/2)]

    med_to_a = a_to_med[::-1]

    # 求边界
    bound = []
    a = single_bound(m,med_to_a,alph)
    b = single_bound(n,med_to_b,alph)
    # bound.append((a,m,n,b))
    bound.append((a, m, b))
    return bound

if __name__ == "__main__":
    # x = [2.5,2.9,1.4,0.8,-4,0,-1,-1.3,4.3,-3.4,-2.2]
    x = [3.9984 ,3.9984 ,3.9984 ,3.9984 ,3.9992 ,3.9992 ,3.9984 ,3.9992 ,3.9992 ]

    b = solve_bound(x,0.5)
    print(b)

